﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class TaskTypeController : Controller
    {
        private DbEntity db = new DbEntity();

        [IsAdmin]
        public ViewResult Index()
        {
            var list = db.TaskTypes.OrderBy(n => n.Sort).ToList();
            return View(list);
        }

        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Create(TaskType tasktype)
        {
            if (ModelState.IsValid)
            {
                //判断是否已经存在
                foreach (var tt in db.TaskTypes)
                {
                    if (tt.TypeName == tasktype.TypeName)
                    {
                        ViewData["error"] = "任务类型已经存在";
                        return View(tasktype);
                    }
                }
                db.TaskTypes.Add(tasktype);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tasktype);
        }

        [IsAdmin]
        public ActionResult Edit(int id)
        {
            TaskType tasktype = db.TaskTypes.Find(id);
            return View(tasktype);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(TaskType tasktype)
        {
            if (ModelState.IsValid)
            {
                //判断是否已经存在
                foreach (var tt in db.TaskTypes)
                {
                    if (tt.TypeName == tasktype.TypeName && tt.TaskTypeID != tasktype.TaskTypeID)
                    {
                        ViewData["error"] = "任务类型已经存在";
                        return View(tasktype);
                    }
                }

                //执行编辑
                var sql = string.Format("update [taskTypes] set [typeName]='{0}' where [TaskTypeID]='{1}'",
                    tasktype.TypeName, tasktype.TaskTypeID);
                var n = db.Database.ExecuteSqlCommand(sql);
                if (n == 0)
                {
                    ViewData["error"] = "保存失败";
                    return View(tasktype);
                }

                return RedirectToAction("Index");
            }
            return View(tasktype);
        }

        //删除类型
        [IsAdmin]
        [HttpPost]
        public ActionResult Delete(int id)
        {
            //Response.ContentType = "application/json";
            var task = db.Tasks.Where(n => n.TaskTypeID == id).Count();
            if (task >0)
                return Json("该任务类型正在使用中,不能删除");
            TaskType tasktype = db.TaskTypes.Find(id);
            db.TaskTypes.Remove(tasktype);
            db.SaveChanges();
            return Json("删除成功");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}